package com.my.myupdata;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

public class SplishActivity extends AppCompatActivity {
    private TextView tvSecond;
    /**
     * 倒计时 单位s
     */
    private int mCount = 3;
    /**
     * 倒计时方法
     */
    private CountDownTimer countDownTimer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splish);

        initView();
    }

    private void initView() {
        tvSecond = findViewById(R.id.tv_second);
        goTimer();
    }

    @Override
    protected void onResume() {
        super.onResume();
        countDownTimer.start();
    }

    /**
     * 倒计时方法
     */
    private void goTimer() {
        countDownTimer = new CountDownTimer(mCount * 1000 + 1050, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                tvSecond.setText("" + (int) (millisUntilFinished / 1000 - 1));
            }

            @Override
            public void onFinish() {
                goIntent();
            }
        };
    }

    private void goIntent() {
        Intent intent = new Intent(SplishActivity.this, MainActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    protected void onStop() {
        super.onStop();
        countDownTimer.cancel();
    }
}
